package com.lonely.fx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.Objects;

/**
 * @author 独孤小标
 * @description 按钮设置
 * @since 2022/6/23 10:04 下午
 */
public class ButtonSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        //创建1个按钮
        Button b1 = new Button();

        //设置按钮上的文字
        b1.setText("这是按钮");

        //设置按钮位置，设置左上角坐标
        b1.setLayoutX(200);
        b1.setLayoutY(100);

        //设置按钮大小
        b1.setPrefWidth(200);
        b1.setPrefHeight(200);

        //设置鼠标放入按钮范围的鼠标形状
        b1.setCursor(Cursor.HAND);

        //设置字体大小
        b1.setFont(Font.font(30));

        //设置字体颜色
        b1.setTextFill(Paint.valueOf("#d3d7d4"));

        //设置边框样式，方法1--使用纯java编写
        BorderStroke borderStroke = new BorderStroke(Paint.valueOf("#f05b72"), BorderStrokeStyle.DASHED,new CornerRadii(30),new BorderWidths(5));
        Border border = new Border(borderStroke);
        b1.setBorder(border);

        //设置背景颜色
        //第一个参数是背景颜色  第二个参数是边框圆弧度 第三个参数是与边框的边距
        //BackgroundFill backgroundFill = new BackgroundFill(Paint.valueOf("#a3cf62"), new CornerRadii(30), new Insets(5));
        //Background background = new Background(backgroundFill);
        //b1.setBackground(background);

        //设置边框样式和背景颜色  方法2--使用fx特有的css编写   fx特有的cxx属性表：
        b1.setStyle(String.format("-fx-background-color:%s;-fx-background-radius:%s;","#a3cf62","30"));

        //设置单击事件
        /*b1.setOnAction(event -> {
            Button button = (Button) event.getSource();
            System.out.printf("有按钮：%s 点击了 \n",button.getText());
        });*/

        //设置鼠标多击事件
        b1.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            //获取点击次数
            int clickCount = event.getClickCount();
            System.out.println("点击次数：" + clickCount);

            //获取是鼠标的哪个键
            MouseButton button = event.getButton();
            System.out.println("鼠标按键："+button.name());

            //案例：鼠标左键双击进入判断
            if(clickCount==2 && MouseButton.PRIMARY == button){
                System.out.println("鼠标左键双击");
            }

        });


        //设置键盘按下未松开事件
        b1.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {

                //获取是哪个按键
                String eventCode = event.getCode().getName();
                System.out.println("按钮被按下：" + eventCode);

                //案例，模拟只监听空格键，其他的忽略
                if(KeyCode.SPACE == event.getCode()){
                    System.out.println("按了空格键");
                }
            }
        });

        //设置键盘松开事件
        b1.setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                KeyCode keyCode = event.getCode();
                System.out.printf("键盘按键：%s 松开 \n",keyCode.getName());
            }
        });



        Group group = new Group();
        group.getChildren().add(b1);
        Scene scene = new Scene(group);

        primaryStage.setScene(scene);
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);

        primaryStage.show();
    }
}
